﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Infrastructure;
using W.Product.API.Appliaction.Command.ProductCommand;

namespace W.Product.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]

    //产品增删改，产品类型增删改控制器
    public class ProductController : ControllerBase
    {
        private readonly IMediator mediator;
        private ILogger<ProductController> logger;

        public ProductController (IMediator mediator, ILogger<ProductController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 添加产品管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddProduct(ProductCreateCommand command)
        {
            ApiResult<int> result=new ApiResult<int>();
            try
            {
                logger.LogInformation("添加产品");
                result.Code = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }
           
        }
        /// <summary>
        /// 修改产品管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpProduct(ProductUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                logger.LogInformation("修改产品");
                result.Code = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }
            
        }

        /// <summary>
        /// 删除产品管理
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelProduct(ProductDeleteCommand product)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                logger.LogInformation("删除产品");
                result.Code = await mediator.Send(product);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }
            
        }
        /// <summary>
        /// 反填产品管理
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<W.Product.Domain.Product.ProductInfo>> FindProduct([FromQuery] ProductFindCommand product)
        {
            ApiResult<W.Product.Domain.Product.ProductInfo> result = new ApiResult<W.Product.Domain.Product.ProductInfo>();
            try
            {
                logger.LogInformation("反填产品管理");
                result.Data = await mediator.Send(product);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }
        }


        /// <summary>
        /// 批量删除产品管理
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> IsDelProduct(ProductIsDelCommand product)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                logger.LogInformation("删除产品");
                result.Code = await mediator.Send(product);
                return result;
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;

                throw;
            }

        }


    }
}
